//
//  COWalletCollectionViewCell.swift
//  CocoProject
//
//  Created by Ruthless on 2023/8/30.
//

import UIKit
import QMUIKit

class COWalletCollectionViewCell: UICollectionViewCell {
    var model: COWalletCellModel? {
        didSet {
            guard let model = model else {
                return
            }
            
            moneyButton.setAttributedTitle(model.money, for: .normal)
            totalButton.setTitle("$\(model.total)", for: .normal)
            
            if model.isNew {
                hintImageView.isHidden = false
                hintImageView.image = UIImage(named: "编组 24")
                hintLabel.isHidden = true
            } else if model.off != 100 {
                hintImageView.isHidden = false
                hintImageView.image = UIImage(named: "矩形 1")
                hintLabel.isHidden = false
                hintLabel.text = "\(100-model.off)OFF%"
            } else {
                hintImageView.isHidden = true
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func configUI() {
        contentView.addSubview(mainView)
        contentView.addSubview(hintImageView)
        hintImageView.addSubview(hintLabel)
        mainView.addSubview(moneyButton)
        mainView.addSubview(totalButton)
        
        mainView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview().offset(0)
            make.top.equalToSuperview().offset(10)
        }
        
        hintImageView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(0)
            make.width.equalTo(65)
            make.height.equalTo(20)
        }
        
        hintLabel.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        moneyButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(21)
        }
        
        totalButton.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(-21)
            make.width.equalTo(80)
            make.height.equalTo(24)
            make.centerX.equalToSuperview()
        }
    }
    
    private lazy var hintImageView: UIImageView = {
        let imgV = UIImageView()
        return imgV
    }()
    
    private lazy var hintLabel: UILabel = {
        let label = UILabel()
        label.font = .medium(12)
        label.textColor = .white
        return label
    }()
    
    private lazy var mainView: UIView = {
        let view = UIView()
        view.layer.masksToBounds = true
        view.layer.cornerRadius = 20
        view.layer.borderColor = UIColor.white.withAlphaComponent(0.2).cgColor
        view.layer.borderWidth = 1
        view.backgroundColor = .rgba(17, 7, 41, 1)
        return view
    }()
    
    private lazy var moneyButton: QMUIButton = {
        let btn = QMUIButton()
        btn.spacingBetweenImageAndTitle = 4
        btn.setImage(UIImage(named: "金币"), for: .normal)
        btn.isUserInteractionEnabled = false
        return btn
    }()
    
    private lazy var totalButton: UIButton = {
        let btn = UIButton()
        btn.backgroundColor = .white.withAlphaComponent(0.1)
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 12
        btn.titleLabel?.font = .medium(12)
        btn.isUserInteractionEnabled = false
        return btn
    }()
}
